package com.bulletelc.apollopre.module.cases;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.CaseListBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.main.center.intention.IntentionActivity;
import com.bulletelc.apollopre.module.web.WebPageActivity;
import com.bulletelc.apollopre.utils.ToastUtil;
import com.bulletelc.apollopre.widget.FoolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 */

public class CaseListActivity extends MVPBaseActivity<CaseListContract.View, CaseListPresenter> implements CaseListContract.View,
        FoolBar.ClickCallback, AdapterView.OnItemClickListener, View.OnClickListener {

    // views
    private FoolBar flBar;
    private ListView lvCase;
    private Button btnDesign;

    // data
    private List<CaseListBean> caseListBeanList;
    private CaseListAdapter adapter;

    @Override
    public int getLayoutRes() {
        return R.layout.case_list_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        lvCase = findView(R.id.lvCase);
        btnDesign = findView(R.id.btnDesign);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
        lvCase.setOnItemClickListener(this);
        btnDesign.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        flBar.setTitle("装修案例");
        caseListBeanList = new ArrayList<>();
        adapter = new CaseListAdapter(this, caseListBeanList);
        lvCase.setAdapter(adapter);
        presenter.getCaseList();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    protected CaseListPresenter createPresenter() {
        return new CaseListPresenter();
    }

    @Override
    public void onSuccessCaseList(List<CaseListBean> caseListBeanList) {
        if (caseListBeanList.size() > 0) {
            this.caseListBeanList.clear();
            this.caseListBeanList.addAll(caseListBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onErrorCaseList(String error) {
        ToastUtil.showLongToast(this,R.string.network_msg);
    }

    @Override
    public void onFailCaseList(String fail) {
        ToastUtil.showLongToast(this, fail);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        int id = caseListBeanList.get(i).getId();
        Intent intent = new Intent(this, WebPageActivity.class);
        intent.putExtra(IntentCode.EXTRA_KEY_URL, NetWorkPath.CASE_DETAIL_URL + "?id=" + id );
        intent.putExtra(IntentCode.EXTRA_WEB_PAGE_FLAG, GlobalConfig.WEB_PAGE_FOR_CESE);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnDesign:
                Intent intent = new Intent(this, IntentionActivity.class);
                startActivity(intent);
                break;
        }
    }
}
